<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Monda:300,300italic,400,400italic,700,700italic|Roboto Slab:300,300italic,400,400italic,700,700italic|PT Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"wangxl12.github.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":true},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":true,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="为什么我们要用git来提交代码？">
<meta property="og:type" content="article">
<meta property="og:title" content="Git版本控制与常见操作">
<meta property="og:url" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/index.html">
<meta property="og:site_name" content="WXL&#39;s blog">
<meta property="og:description" content="为什么我们要用git来提交代码？">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E7%A4%BA%E6%84%8F%E5%9B%BE.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E5%A4%8D%E6%9D%82%E5%9B%BE.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E4%BB%93%E5%BA%93%E5%9B%BE.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/nostaged.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/diff.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/nodiff.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/deletestatus.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/newstatus.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/result1.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/delete.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/commit.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E6%B7%BB%E5%8A%A0%E4%BA%86%E4%B8%80%E8%A1%8C.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E4%BF%AE%E6%94%B9%E6%B6%88%E5%A4%B1.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%811.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E5%90%88%E5%B9%B6.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%BB%93%E6%9E%9C2.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E5%B9%B6%E8%A1%8C%E7%8A%B6%E6%80%81.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/solveconflict.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%812.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%813.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/test%E6%96%87%E4%BB%B6%E5%8F%88%E6%9C%89%E4%BA%86.png">
<meta property="og:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%814.png">
<meta property="article:published_time" content="2021-11-14T11:44:00.000Z">
<meta property="article:modified_time" content="2021-11-14T12:20:26.149Z">
<meta property="article:author" content="WXL">
<meta property="article:tag" content="Git">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E7%A4%BA%E6%84%8F%E5%9B%BE.png">

<link rel="canonical" href="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>Git版本控制与常见操作 | WXL's blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="WXL's blog" type="application/atom+xml">
<link href="https://cdn.bootcss.com/KaTeX/0.11.1/katex.min.css" rel="stylesheet" /></head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">WXL's blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Talk is cheap, show me your work.</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">33</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">49</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/wangxl12" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/td.jpg">
      <meta itemprop="name" content="WXL">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="WXL's blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Git版本控制与常见操作
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-11-14 19:44:00 / 修改时间：20:20:26" itemprop="dateCreated datePublished" datetime="2021-11-14T19:44:00+08:00">2021-11-14</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Git/" itemprop="url" rel="index"><span itemprop="name">Git</span></a>
                </span>
            </span>

          
            <span id="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/" class="post-meta-item leancloud_visitors" data-flag-title="Git版本控制与常见操作" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>为什么我们要用git来提交代码？</p>
<span id="more"></span>
<p>我们平常在自己电脑上写一个简单的实验题目的时候，比如我实现了可视化功能，代码以及没有bug了，接下来想要同步数据库，但是又害怕接下来的代码会误改之前完整的代码，于是我就将当前没有Bug的代码压缩打包，命名为v1.0，然后就可以肆无忌惮的编写后面的代码了，等到后面的某一个功能实现之后，我重新将新版的代码压缩打包，命名为v1.1，继续后面的工作，如果后面某一个环节没做好导致所有代码都崩了，想回到v1.0，直接解压v1.0压缩包重新开始编写即可。</p>
<p>相信大家遇到一个复杂的项目的时候会有上述的操作，但是操作比较繁杂，特别是多个人来合作一个项目开发的时候，大家的代码共享、修改都会遇到很多的问题。使用git可以很好的解决上述问题，大家可以在自己的电脑上写好代码，然后上传到github 上，也可以将别人写的代码从github上下载过来，这个功能解决了开发人员之间的代码交互问题。</p>
<p>GitHub支持多个版本分支，如下图：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E7%A4%BA%E6%84%8F%E5%9B%BE.png" alt="版本控制意图"></p>
<p>上图中每一个节点表示一个版本号的代码，假设左边分支名字为dev，表示我们进行开发的分支，也就是可能有bug，正在维护的版本分支；右边分支假设是main 分支，表示我们发布的版本分支，比如我们最终可以拿出来发布的版本。</p>
<p><code>main</code>分支应该是非常稳定的，也就是仅用来发布新版本，平时不能在上面干活；那在哪干活呢？干活都在<code>dev</code>分支上，也就是说，<code>dev</code>分支是不稳定的，到某个时候，比如1.0版本发布时，再把<code>dev</code>分支合并到<code>main</code>上，在<code>main</code>分支发布1.0版本.团队合作的分支看起来就像这样：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E5%A4%8D%E6%9D%82%E5%9B%BE.png" alt="复杂图"></p>
<p>我首先在我的账号上创建了一个仓库<code>BigData</code>，然后拉其他人过来就可以协同完成这个项目，首先将项目从远程拉取过来：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone git@github.com:wangxl12/BigDataProject.git</span><br></pre></td></tr></table></figure>
<p>注意要把SSH Key添加到GitHub，如果提示电脑没有安装ssh，就需要安装一下.</p>
<p>查看目前有哪些分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch</span><br></pre></td></tr></table></figure>
<p>默认只有main分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git branch</span></span><br><span class="line">* main</span><br></pre></td></tr></table></figure>
<p>注意，要在dev分支上开发，就必须创建远程origin（远程主机名）的dev分支到本地，使用如下命令创建本地dev分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git switch -c dev origin/dev</span><br></pre></td></tr></table></figure>
<p>然后就可以在dev分支上修改，并时不时将dev分支push到远程：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git add .</span><br><span class="line">git commit -m &quot;changed something&quot;</span><br><span class="line">git push origin dev</span><br></pre></td></tr></table></figure>
<p>我一开始创建仓库的时候只有main分支，这个分支作为稳定版本发布的分支，尽量少用，而dev分支我们作为开发分支，可以上传代码到这里。可能对于本地仓库、远程仓库的概念有不理解的地方，可以参看下面这张图：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E4%BB%93%E5%BA%93%E5%9B%BE.png" alt="仓库图"></p>
<p>workspace就是我们的本地工作区，即BigData文件夹，index是暂存区，也在本地，repository是本地仓库，就是BigData目录下的.git目录（这是一个隐藏目录，如果看不见的话可能隐藏了，可以设置文件夹中的隐藏项为可见就可以看到了），而Remote就是远程仓库，即Github或者Gitee。有向线段之间的单词表示操作，经过这些操作之后文件会被传到不同的仓库。</p>
<h1>廖雪峰教程</h1>
<p>工作区（BigData）目录下有一个隐藏目录.git，这个是Git的版本库，版本库里有很多东西，其中最重要的就是称为stage（或者叫index)的暂存区，还有Git为我们自动创建的第一个分支master，以及指向master的一个指针叫HEAD.</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93.png" alt="本地仓库"></p>
<p>往Git版本库里添加的时候分为两步：</p>
<p>第一步是用<code>git add</code>将文件添加进去，实际上就是将文件中的修改添加到暂存区；</p>
<p>第二步是用<code>git commit</code>提交修改，实际上是将暂存区中的所有内容提交到当前分支；</p>
<p>因为我们创建Git版本库时，Git自动为我们创建了唯一一个<code>master</code>分支，所以，现在，<code>git commit</code>就是往<code>master</code>分支上提交更改。可以简单理解为，需要提交的文件修改通通放到暂存区，然后，一次性提交暂存区的所有修改。</p>
<p>比如现在创建了一个test.txt文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">touch test.txt</span><br></pre></td></tr></table></figure>
<p>先加到暂存区：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git add test.txt</span><br></pre></td></tr></table></figure>
<p>然后提交到当前分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git commit -m &quot;我创建了一个test.txt文件&quot;</span><br></pre></td></tr></table></figure>
<p>注意，<code>git commit</code>只会将暂存区里的修改提交到当前的分支，如果有修改没有提交到暂存区，那么<code>commit</code>无法将这个修改提交到当前分支。此时，使用<code>git status</code>查看当前的状态的时候，可以看见类似下面的内容：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/nostaged.png" alt="nostaged"></p>
<p>这就说明有一个read.txt文件在暂存区内，该文件有修改没有提交到版本库。可以使用如下指令<strong>查看工作区内的这个文件和版本库（本地）之间的区别</strong>：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git diff HEAD -- readme.txt</span><br></pre></td></tr></table></figure>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/diff.png" alt="diff"></p>
<p>白色是工作区和版本库之间相同的部分，红色是工作区删掉的，绿色则是工作区添加的内容。</p>
<h1>丢弃修改</h1>
<ul>
<li><strong>假设此时修改了工作区的内容，但是还没有提交到暂存区，可以通过如下的方式丢弃修改：</strong></li>
</ul>
<p><strong>丢弃工作区的修改：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git restore readme.txt  # 不用背，使用git status查看状态的时候有提示</span><br></pre></td></tr></table></figure>
<p>修改之后查看工作区和版本库之间的区别：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/nodiff.png" alt="nodiff"></p>
<p>发现回到了修改工作区之前的状态。就是让这个文件回到最近一次<code>git commit</code>或<code>git add</code>时的状态。<code>git checkout -- file</code>命令中的<code>--</code>很重要，没有<code>--</code>，就变成了“切换到另一个分支”的命令。</p>
<ul>
<li><strong>假设此时修改了工作区的内容，还通过add添加到了暂存区，可以通过如下的方式丢弃修改：</strong></li>
</ul>
<p>首先输入<code>git restore --staged &lt;file&gt;</code>将暂存区中的文件恢复到提交到暂存区之前，即在工作区修改了没有进行add操作。</p>
<p>然后使用<code>git restore &lt;file&gt;</code>将工作区的文件修改抛弃，即恢复到没有修改的状态。期间可以使用git status查看文件的状态，<strong>如果是绿色字体的modified，表示提交到了暂存区，没有commit，如果是红色的modified，表示修改了没有add。</strong></p>
<ul>
<li><strong>如果不但add交到了暂存区，还commit到了版本库，可以使用版本回退来回到上一个版本，不过这个要求你还没有将本地的版本库推送到远程版本库。</strong></li>
</ul>
<p>然后将本地仓库的内容上传到远程仓库：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push</span><br></pre></td></tr></table></figure>
<p>查看历史记录：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git log  # 确定要回到之前的哪个版本</span><br><span class="line"><span class="meta">#</span><span class="bash"> 或者</span></span><br><span class="line">git log --pretty=oneline</span><br></pre></td></tr></table></figure>
<p>版本回退：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard HEAD^  # 回退一个版本</span><br><span class="line">git reset --hard HEAD^^^ # 回退3个版本</span><br><span class="line">git reset --hard HEAD~100 # 回退100个版本</span><br></pre></td></tr></table></figure>
<p>版本前进：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reset --hard 版本号  # 版本号可以通过git log查看，不需要完全输入，可以通过输入4~5个字符自动找到</span><br></pre></td></tr></table></figure>
<p>查看记录的每一次命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git reflog  # 确定要回到未来的哪个版本</span><br></pre></td></tr></table></figure>
<h1>删除文件</h1>
<p>将readme.txt删除：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rm readme.txt</span><br></pre></td></tr></table></figure>
<p>然后查看状态：<code>git status</code></p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/deletestatus.png" alt="deletestatus"></p>
<p>根据提示，可以使用<code>git restore &lt;file&gt;</code>来恢复工作区的修改：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git restore readme.txt</span><br></pre></td></tr></table></figure>
<p>重新查看状态：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git status</span><br></pre></td></tr></table></figure>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/newstatus.png" alt="newstatus"></p>
<p>但是如果真的想删掉呢？我们创建一个test.txt文件试一下</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">touch test.txt</span><br><span class="line">git add .</span><br><span class="line">git commit -m &quot;commit test.txt&quot;</span><br><span class="line">rm test.txt</span><br><span class="line">git status</span><br></pre></td></tr></table></figure>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/result1.png" alt="result1"></p>
<p>接下来将暂存区中的文件删掉，然后commit即可：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/delete.png" alt="delete"></p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/commit.png" alt="commit"></p>
<h1>创建远程仓库</h1>
<p>如果一开始只是在本地仓库中操作的，可以将本地仓库关联远程 仓库，然后将本地仓库的内容推送到GitHub仓库，在本地仓库下执行如下指令即可关联：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add origin git@github.com:wangxl12/learngit.git</span><br></pre></td></tr></table></figure>
<p>这里的origin是远程仓库，这是Git默认的叫法，也可以改成别的，但是<code>origin</code>这个名字一看就知道是远程库。</p>
<p>下一步可以将本地库中的所有内容推送到远程库上：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -u origin master</span><br></pre></td></tr></table></figure>
<p>把本地库的内容推送到远程，用<code>git push</code>命令，实际上是把当前分支<code>master</code>推送到远程。</p>
<p>由于远程库是空的，我们第一次推送<code>master</code>分支时，加上了<code>-u</code>参数，Git不但会把本地的<code>master</code>分支内容推送的远程新的<code>master</code>分支，还会把本地的<code>master</code>分支和远程的<code>master</code>分支关联起来，在以后的推送或者拉取时就可以简化命令。</p>
<p>从现在起，只要本地作了提交，就可以通过命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin master</span><br></pre></td></tr></table></figure>
<p>把本地<code>master</code>分支的最新修改推送至GitHub，现在，你就拥有了真正的分布式版本库！</p>
<h1>删除远程库</h1>
<p>如果添加的时候地址写错了，或者就是想删除远程库，可以用<code>git remote rm &lt;name&gt;</code>命令。使用前，建议先用<code>git remote -v</code>查看远程库信息：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git remote -v</span></span><br><span class="line">origin  git@github.com:wangxl12/BigDataProject.git (fetch)</span><br><span class="line">origin  git@github.com:wangxl12/BigDataProject.git (push)</span><br></pre></td></tr></table></figure>
<p>然后，根据名字删除，比如删除<code>origin</code>：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote rm origin</span><br></pre></td></tr></table></figure>
<p>此处的“删除”其实是解除了本地和远程的绑定关系，并不是物理上删除了远程库。远程库本身并没有任何改动。要真正删除远程库，需要登录到GitHub，在后台页面找到删除按钮再删除。</p>
<h1>从远程库克隆</h1>
<p>上面将的是先创建本地库，然后关联远程库，现在是先创建远程库，然后从远程库克隆。</p>
<p>如果有多个人协作开发，那么每个人各自从远程克隆一份就可以了。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone git@github.com:michaelliao/gitskills.git</span><br></pre></td></tr></table></figure>
<h1>分支管理</h1>
<h2 id="创建与合并分支">创建与合并分支</h2>
<p>强烈建议先学廖雪峰的Git教程：<a target="_blank" rel="noopener" href="https://www.liaoxuefeng.com/wiki/896043488029600/900003767775424">https://www.liaoxuefeng.com/wiki/896043488029600/900003767775424</a></p>
<p><strong>创建分支并切换分支：</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b dev</span><br><span class="line"><span class="meta">#</span><span class="bash"> 相当于下面的两句：</span></span><br><span class="line">git branch dev</span><br><span class="line">git checkout dev</span><br></pre></td></tr></table></figure>
<p>如果希望将分支推送到远程库可以使用如下指令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin dev</span><br></pre></td></tr></table></figure>
<p>使用<code>git branch</code>查看分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch</span><br></pre></td></tr></table></figure>
<p>然后我们可以在dev分支上正常提交，比如对readme.txt添加了一行内容：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E6%B7%BB%E5%8A%A0%E4%BA%86%E4%B8%80%E8%A1%8C.png" alt="添加了一行"></p>
<p>绿色部分就是添加的内容，然后提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add readme.txt</span><br><span class="line">git commit -m &quot;branch dev test&quot;</span><br></pre></td></tr></table></figure>
<p>通过git status也可以查看状态信息。</p>
<p>然后dev分支的工作完成之后，可以切回到main分支并查看添加的内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git checkout main</span><br><span class="line">cat readme.txt</span><br></pre></td></tr></table></figure>
<p>发现刚刚添加的内容不见了:</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E4%BF%AE%E6%94%B9%E6%B6%88%E5%A4%B1.png" alt="修改消失"></p>
<p>因为刚刚提交的是在dev 分支上，而main 分支此时的提交点并没有发生改变：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%811.png" alt="状态1"></p>
<p>现在，我们将dev分支的工作成果合并到main上：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git merge dev</span><br></pre></td></tr></table></figure>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E5%90%88%E5%B9%B6.png" alt="合并"></p>
<p>再查看readme.txt内容发现和dev分支的内容一致了：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%BB%93%E6%9E%9C2.png" alt="结果2"></p>
<p>注意到上面的<code>Fast-forward</code>信息，Git告诉我们，这次合并是“快进模式”，也就是直接把<code>master</code>指向<code>dev</code>的当前提交，所以合并速度非常快。</p>
<p>当然，也不是每次合并都能<code>Fast-forward</code>，我们后面会讲其他方式的合并。</p>
<p>合并完成后，就可以放心地删除<code>dev</code>分支了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git branch -d dev</span><br><span class="line"><span class="meta">#</span><span class="bash"> 查看分支：</span></span><br><span class="line">git branch</span><br></pre></td></tr></table></figure>
<p>因为创建、合并和删除分支非常快，所以Git鼓励你使用分支完成某个任务，合并后再删掉分支，这和直接在<code>master</code>分支上工作效果是一样的，但过程更安全。</p>
<p>切换分支还可以使用switch：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span><span class="bash"> 创建并切换分支</span></span><br><span class="line">git switch -c dev</span><br><span class="line"><span class="meta">#</span><span class="bash"> 切换到已有分支</span></span><br><span class="line">git switch dev</span><br></pre></td></tr></table></figure>
<p>查看分支：<code>git branch</code></p>
<p>创建分支：<code>git branch &lt;name&gt;</code></p>
<p>切换分支：<code>git checkout &lt;name&gt;</code>或者<code>git switch &lt;name&gt;</code></p>
<p>创建+切换分支：<code>git checkout -b &lt;name&gt;</code>或者<code>git switch -c &lt;name&gt;</code></p>
<p>合并某分支到当前分支（快速合并）：<code>git merge &lt;name&gt;</code></p>
<p>删除分支：<code>git branch -d &lt;name&gt;</code></p>
<h2 id="解决冲突">解决冲突</h2>
<p>现在只有main分支，创建一个文件test，add、commit，然后创建一个新的分支feature1，add到暂存区，切换到这个分支下commit到本地仓库中：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">touch test</span><br><span class="line">git add .</span><br><span class="line"><span class="meta">#</span><span class="bash"> 先将<span class="built_in">test</span>创建的这一个修改提交到本地仓库</span></span><br><span class="line">git commit -m &quot;create test file&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 创建并切换到feature1分支</span></span><br><span class="line">git switch -c feature1</span><br><span class="line"><span class="meta">#</span><span class="bash"> *********向<span class="built_in">test</span>文件中添加一行内容，比如我添加branch main**************</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> vi <span class="built_in">test</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> i</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> branch main</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> :wq!</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> *****************************************************************</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> .表示将所有修改提交到暂存区</span></span><br><span class="line">git add .</span><br><span class="line"><span class="meta">#</span><span class="bash"> 暂存区和工作区都是所有分支共享的，即修改和add操作在哪个分支下进行都可</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> 但是commit需要切换到想要提交的分支，这里我想提交到main分支</span></span><br><span class="line">git switch main</span><br><span class="line">git commit -m &quot;add a main branch line&quot;</span><br><span class="line"><span class="meta">#</span><span class="bash"> 至此，main分支已经比feature1分支多一个版本节点了，下面继续让二者程并列的状态</span></span><br><span class="line">git switch feature1</span><br><span class="line"><span class="meta">#</span><span class="bash"> *********向<span class="built_in">test</span>文件中添加一行内容，比如我添加的是branch feature1*******</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> vi <span class="built_in">test</span>  <span class="comment"># 打开会发现test是空的，因为之前的修改已经提交到main分支上了</span></span></span><br><span class="line"><span class="meta">#</span><span class="bash"> i</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> line2 branch dev</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> :wq!</span></span><br><span class="line"><span class="meta">#</span><span class="bash"> *****************************************************************</span></span><br><span class="line">git add .</span><br><span class="line">git commit -m &quot;add a feature1 branch line&quot;</span><br><span class="line">git switch main</span><br></pre></td></tr></table></figure>
<p>至此，分支的状态到达了下图所示：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E5%B9%B6%E8%A1%8C%E7%8A%B6%E6%80%81.png" alt="并行状态"></p>
<p>这种情况下，Git无法执行“快速合并”，只能试图把各自的修改合并起来，但这种合并就可能会有冲突，我们试试看：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git merge feature1</span></span><br><span class="line">Auto-merging test</span><br><span class="line">CONFLICT (content): Merge conflict in test</span><br><span class="line">Automatic merge failed; fix conflicts and then commit the result.</span><br></pre></td></tr></table></figure>
<p>因为我们对一个文件进行了两种修改，这两种修改谁覆盖谁都说不通，所以会发生冲突。我们查看冲突的文件：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git status</span></span><br><span class="line">On branch main</span><br><span class="line">Your branch is ahead of &#x27;origin/main&#x27; by 11 commits.</span><br><span class="line">  (use &quot;git push&quot; to publish your local commits)</span><br><span class="line"></span><br><span class="line">You have unmerged paths.</span><br><span class="line">  (fix conflicts and run &quot;git commit&quot;)</span><br><span class="line">  (use &quot;git merge --abort&quot; to abort the merge)</span><br><span class="line"></span><br><span class="line">Unmerged paths:</span><br><span class="line">  (use &quot;git add &lt;file&gt;...&quot; to mark resolution)</span><br><span class="line">        both modified:   test</span><br><span class="line"></span><br><span class="line">no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>我们直接查看test的内容：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> cat <span class="built_in">test</span></span></span><br><span class="line">&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD</span><br><span class="line">branch main</span><br><span class="line">=======</span><br><span class="line">line2 branch dev</span><br><span class="line"><span class="meta">&gt;</span><span class="bash">&gt;&gt;&gt;&gt;&gt;&gt; feature1</span></span><br></pre></td></tr></table></figure>
<p>我们直接手动修改test文件为如下内容，从而解决冲突：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/solveconflict.png" alt="solveconflict"></p>
<p>然后重新提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git add test</span><br><span class="line">git commit -m &quot;conflict fixed&quot;</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git commit -m <span class="string">&quot;conflict fixed&quot;</span></span></span><br><span class="line">[main d7c72cc] conflict fixed</span><br></pre></td></tr></table></figure>
<p>现在main和feature1分支变成了下图所示：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%812.png" alt="状态2"></p>
<p>用带参数的<code>git log</code>也可以看到分支的合并情况：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">log</span> --graph --pretty=oneline --abbrev-commit</span></span><br><span class="line">*   d7c72cc (HEAD -&gt; main) conflict fixed</span><br><span class="line">|\</span><br><span class="line">| * 7fb2bc5 (feature1) add a branch feature1 line</span><br><span class="line">* | f68248d add a main branch line</span><br><span class="line">|/</span><br><span class="line">*   69e37a8 Merge branch &#x27;dev&#x27; into main</span><br><span class="line">|\</span><br><span class="line">| * d441a47 test</span><br><span class="line">* | 4e52e8e test-main</span><br><span class="line">|/</span><br><span class="line">* 9bea0ff branch dev test</span><br><span class="line">* 53a40b8 delete test.txt</span><br><span class="line">* a66d7e5 commit test.txt</span><br><span class="line">* 4b238b8 test</span><br><span class="line">* b5353dc two lines changed</span><br><span class="line">* 80c715c add line 2</span><br><span class="line">* 87cf69b create readme.txt</span><br><span class="line">* ed46945 (origin/main) delete README.md</span><br><span class="line">* f650647 (origin/origin/dev, origin/dev) wxl commit readme.md</span><br></pre></td></tr></table></figure>
<p>合并完成之后，我们就可以删除feature1分支了：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -d feature1</span><br></pre></td></tr></table></figure>
<p>当Git无法自动合并分支时，就必须首先解决冲突。解决冲突后，再提交，合并完成。</p>
<p>解决冲突就是把Git合并失败的文件手动编辑为我们希望的内容，再提交。</p>
<p>用<code>git log --graph</code>命令可以看到分支合并图。</p>
<p><strong>上面举的例子是对同一个文件进行修改，所以无法自动合并，如果是两个分支同时创建一个文件的话，还是可以使用merge合并的，下面的内容就是举的这个例子：</strong></p>
<p>然后再新建一个文件，add,切换到main分支下commit：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">touch test-main</span><br><span class="line">git add .</span><br><span class="line">git switch main</span><br><span class="line">git commit -m &quot;test-main&quot;</span><br></pre></td></tr></table></figure>
<p>注意这里的创建文件、add在哪个分支下进行都可以，因为工作区和暂存区是所有分支共享的，但是commit需要在想提交的那个分支下进行，因为commit的本质就是同步工作区里的相应的分支。</p>
<p>现在可以切换到dev和main分支中查看，分别有一个test文件和test-main文件。现在各自的状态如下（将feature1分支改为dev，master分支改为main)：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%813.png" alt="状态3"></p>
<p>这种情况下如果进行快速合并的话：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git switch main</span><br><span class="line">git merge dev</span><br></pre></td></tr></table></figure>
<p>会调到一个类似文本文件的界面，输入:q退出。出来之后发现main分支有test文件了：</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/test%E6%96%87%E4%BB%B6%E5%8F%88%E6%9C%89%E4%BA%86.png" alt="test文件又有了"></p>
<h2 id="分支管理策略">分支管理策略</h2>
<p>通常，合并分支时，如果可能，Git会用<code>Fast forward</code>模式，但这种模式下，删除分支后，会丢掉分支信息。</p>
<p>如果要强制禁用<code>Fast forward</code>模式，Git就会在merge时生成一个新的commit，这样，从分支历史上就可以看出分支信息。</p>
<p>合并分支时，加上<code>--no-ff</code>参数就可以用普通模式合并，合并后的历史有分支，能看出来曾经做过合并，而<code>fast forward</code>合并就看不出来曾经做过合并。</p>
<p>切换到dev分支，修改一下test里的内容，然后add, commit提交：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">git switch -c dev</span><br><span class="line">vi test</span><br><span class="line">git add .</span><br><span class="line">git commit -m &quot;add merge&quot;</span><br></pre></td></tr></table></figure>
<p>然后切换回main：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git switch main</span><br></pre></td></tr></table></figure>
<p>准备合并dev分支，请注意–no-ff参数，表示禁用Fast forward:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">git switch main</span><br><span class="line">git merge --no-ff -m &quot;merge with no-ff&quot; dev</span><br></pre></td></tr></table></figure>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git merge --no-ff -m <span class="string">&quot;merge with no-ff&quot;</span> dev  <span class="comment"># 因为本次合并要创建一个新的commit，所以加上-m参数，把commit描述写进去。</span></span></span><br><span class="line">Merge made by the &#x27;recursive&#x27; strategy.</span><br><span class="line"> test | 3 +--</span><br><span class="line"> 1 file changed, 1 insertion(+), 2 deletions(-)</span><br></pre></td></tr></table></figure>
<p>合并后，我们用<code>git log</code>看看分支历史：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> git <span class="built_in">log</span> --graph --pretty=oneline --abbrev-commit</span></span><br><span class="line">*   fecae9f (HEAD -&gt; main) merge with no-ff</span><br><span class="line">|\</span><br><span class="line">| * 39ba997 (dev) add merge</span><br><span class="line">|/</span><br><span class="line">*   d7c72cc (origin/main) conflict fixed</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>可以看到，不使用<code>Fast forward</code>模式，merge后就像这样:</p>
<p><img src="/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/%E7%8A%B6%E6%80%814.png" alt="状态4"></p>

    </div>

    
    
    
        <div class="reward-container">
  <div>行行好，赏一杯咖啡吧~</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="WXL 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="WXL 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>WXL
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://wangxl12.github.io/2021/11/14/Git/Git%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E4%B8%8E%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C/" title="Git版本控制与常见操作">https://wangxl12.github.io/2021/11/14/Git/Git版本控制与常见操作/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/Git/" rel="tag"><i class="fa fa-tag"></i> Git</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/11/14/Git/Git%E5%AE%89%E8%A3%85+%E8%B4%A6%E6%88%B7%E6%B3%A8%E5%86%8C+SourceTree/" rel="prev" title="Git安装+账户注册+SourceTree">
      <i class="fa fa-chevron-left"></i> Git安装+账户注册+SourceTree
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/11/14/Git/%E5%B0%8F%E7%BB%84%E5%90%88%E4%BD%9C%E9%A1%B9%E7%9B%AE%E4%BD%BF%E7%94%A8Git%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF/" rel="next" title="小组合作项目使用Git的合理姿势">
      小组合作项目使用Git的合理姿势 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">1.</span> <span class="nav-text">廖雪峰教程</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">2.</span> <span class="nav-text">丢弃修改</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">3.</span> <span class="nav-text">删除文件</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">4.</span> <span class="nav-text">创建远程仓库</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">5.</span> <span class="nav-text">删除远程库</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">6.</span> <span class="nav-text">从远程库克隆</span></a></li><li class="nav-item nav-level-1"><a class="nav-link"><span class="nav-number">7.</span> <span class="nav-text">分支管理</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9B%E5%BB%BA%E4%B8%8E%E5%90%88%E5%B9%B6%E5%88%86%E6%94%AF"><span class="nav-number">7.1.</span> <span class="nav-text">创建与合并分支</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A7%A3%E5%86%B3%E5%86%B2%E7%AA%81"><span class="nav-number">7.2.</span> <span class="nav-text">解决冲突</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E6%94%AF%E7%AE%A1%E7%90%86%E7%AD%96%E7%95%A5"><span class="nav-number">7.3.</span> <span class="nav-text">分支管理策略</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="WXL"
      src="/images/td.jpg">
  <p class="site-author-name" itemprop="name">WXL</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">49</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">33</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/wangxl12" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wangxl12" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:wxl.1.2.3@qq.com" title="E-Mail → mailto:wxl.1.2.3@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://blog.csdn.net/weixin_43141320?spm=1000.2115.3001.5343" title="https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_43141320?spm&#x3D;1000.2115.3001.5343" rel="noopener" target="_blank">My CSDN Blog</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://blog.kilig.ink/" title="http:&#x2F;&#x2F;blog.kilig.ink&#x2F;" rel="noopener" target="_blank">HuangPiSong</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">WXL</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>


  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  
      

<script>
  if (typeof MathJax === 'undefined') {
    window.MathJax = {
      loader: {
          load: ['[tex]/mhchem'],
        source: {
          '[tex]/amsCd': '[tex]/amscd',
          '[tex]/AMScd': '[tex]/amscd'
        }
      },
      tex: {
        inlineMath: {'[+]': [['$', '$']]},
          packages: {'[+]': ['mhchem']},
        tags: 'ams'
      },
      options: {
        renderActions: {
          findScript: [10, doc => {
            document.querySelectorAll('script[type^="math/tex"]').forEach(node => {
              const display = !!node.type.match(/; *mode=display/);
              const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display);
              const text = document.createTextNode('');
              node.parentNode.replaceChild(text, node);
              math.start = {node: text, delim: '', n: 0};
              math.end = {node: text, delim: '', n: 0};
              doc.math.push(math);
            });
          }, '', false],
          insertedScript: [200, () => {
            document.querySelectorAll('mjx-container').forEach(node => {
              let target = node.parentNode;
              if (target.nodeName.toLowerCase() === 'li') {
                target.parentNode.classList.add('has-jax');
              }
            });
          }, '', false]
        }
      }
    };
    (function () {
      var script = document.createElement('script');
      script.src = '//cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js';
      script.defer = true;
      document.head.appendChild(script);
    })();
  } else {
    MathJax.startup.document.state(0);
    MathJax.texReset();
    MathJax.typeset();
  }
</script>

    

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 's5o4gRGNyYbPVRfziI5EzhO1-gzGzoHsz',
      appKey     : 'N7aQtR2SU9AwgaO2YtVhRO0W',
      placeholder: "发表你的评论吧~",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
